<?php
use \Model\Account;
class Controller_Bind extends Controller_Weibo{
	public function action_portal() {
		$account = Account::get_account(Session::get('user_id', 0));
		$this->data['content'] = View::forge('bind/content', array('account' => $account));
		return Response::forge(View::forge('layout', $this->data));
	}

	public function action_cb() {
		$o = new SaeTOAuthV2( Config::get('weibo.WB_AKEY', ''), Config::get('weibo.WB_SKEY', ''));
		try {
			$username = Input::post('account', '');
			$password = Input::post('password', '');
			$token = $o->getAccessToken('password', array('username' => $username, 'password' => $password));
		} catch (Exception $e){}
		if (isset($token['access_token']) and $token['access_token']) {
			$c = new SaeTClientV2( $app_key, $secret_key, $token['access_token']);
			$user_message = $c->show_user_by_id( $token['uid']);//根据ID获取用户等基本信息
			if (!isset($user_message['name']) or !$user_message['name']) {
				return self::msg(array('msg' => 'fail', 'data' => array()));
			}
			$data['name'] = $user_message['name'];
			$data['screen_name'] = $user_message['screen_name'];
			$data['idstr'] = $user_message['idstr'];
			$data['profile_image_url'] = $user_message['profile_image_url'];
			$data['url'] = $user_message['url'];
			$data['location'] = $user_message['location'];
			$data['description'] = $user_message['description'];
			$data['user_id'] = Session::get('user_id', 0);
			$data['access_token'] = $token['access_token'];
			$data['password'] = $password;
			$data['username'] = $username;
			Account::add($data);
			return self::msg(array('msg' => 'succ', 'data' => $data));
		} else {
			return self::msg(array('msg' => 'fail', 'data' => array()));
		}
	}
}
